﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Configuration;
using LeadExpress.Models.ViewPages;

namespace LeadExpress.Areas.Management.Controllers
{
    public class BusinessListController : Controller
    {
        private static string fileName = "";
        public static string staFileName
        {
            get { return fileName; }
            set { fileName = value; }
        }

        // Create an instance of user manage
        LeadExpress.Repository.BusinessManagementRepository repository = new LeadExpress.Repository.BusinessManagementRepository();
        public static string BusinessImagesURL = ConfigurationManager.AppSettings["BusinessImagesURL"].ToString();
        //
        // GET: /Management/StateList/
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Index()
        {
            BusinessManagementModel model = new BusinessManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator");

            return View(model);
        }

        //
        // GET: /Management/BusinessList/Edit/id
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Add()
        {
            BusinessManagementModel model = new BusinessManagementModel();
            model = repository.GetModelAdd(Helpers.Constant.HOME, "SuperAdministrator");

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Add(FormCollection form)
        {
            Business model = new Business();

            model.CompanyName = form["CompanyName"].ToString();
            model.ContactTitle = form["ContactTitle"].ToString();
            model.BusinessTypeID = int.Parse(form["BusinessType"].ToString());
            model.Address1 = form["Address1"].ToString();
            model.Address2 = form["Address2"].ToString();
            model.City = form["City"].ToString();
            model.State = form["State"].ToString();
            model.ZipCode = form["ZipCode"].ToString();
            model.Phone = form["Phone"].ToString();
            model.Fax = form["Fax"].ToString();
            model.EmailAddress = form["EmailAddress"].ToString();
            model.ImageURL = form["ImageUrl"].ToString();
            model.Active = (form["Active"] != null && form["Active"].ToString() == "on");
            model.WebsiteURL = form["WebsiteURL"].ToString();
            model.CreateByUserID = new Repository.UserRepository().GetSingle(User.Identity.Name).ID;

            repository.Create(model);

            return RedirectToAction("Index", "BusinessList");
        }

        //
        // GET: /Management/BusinessList/Edit/id
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Edit(int id)
        {
            BusinessManagementModel model = new BusinessManagementModel();
            model = repository.GetModelEdit(Helpers.Constant.HOME, "SuperAdministrator", id);
            ViewBag.BusinessId = id;

            return View(model);
        }

        //
        // GET: /Management/BusinessList/Edit/id
        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Edit(int id, FormCollection form)
        {
            Business model = repository.GetSingle(id);

            if (model != null)
            {
                model.CompanyName = form["CompanyName"].ToString();
                model.ContactTitle = form["ContactTitle"].ToString();
                model.BusinessTypeID = int.Parse(form["BusinessType"].ToString());
                model.Address1 = form["Address1"].ToString();
                model.Address2 = form["Address2"].ToString();
                model.City = form["City"].ToString();
                model.State = form["State"].ToString();
                model.ZipCode = form["ZipCode"].ToString();
                model.Phone = form["Phone"].ToString();
                model.Fax = form["Fax"].ToString();
                model.EmailAddress = form["EmailAddress"].ToString();
                model.ImageURL = form["ImageUrl"].ToString();
                model.Active = (form["Active"] != null && form["Active"].ToString() == "on");
                model.WebsiteURL = form["WebsiteURL"].ToString();
                model.CreateByUserID = new Repository.UserRepository().GetSingle(User.Identity.Name).ID;

                repository.Save(model);
            }

            return RedirectToAction("Index", "BusinessList");
        }

        // Delete Email Item
        [HttpGet]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Delete(int id)
        {
            repository.Delete(id);

            BusinessManagementModel model = new BusinessManagementModel();
            model = repository.GetModelDelete(Helpers.Constant.HOME, "SuperAdministrator");

            return Json(model, JsonRequestBehavior.AllowGet);
        }
        //[HttpPost]
        public ActionResult Upload(HttpPostedFileBase fileData)
        {
            // Verify that the user selected a file
            UserModel model = new UserModel();
            if (fileData != null && fileData.ContentLength > 0)
            {
                // extract only the fielname
                fileName = Path.GetFileName(fileData.FileName);

                // store the file inside ~/App_Data/uploads folder
                var path = Path.Combine(Server.MapPath(BusinessImagesURL), fileName);
                fileData.SaveAs(path);
            }
            // redirect back to the index action to show the form once again
            return Json(model);
        }

        [HttpGet]
        public ActionResult UploadComplete()
        {
            return Json(string.Format("{0}/{1}", BusinessImagesURL, fileName), JsonRequestBehavior.AllowGet);
        }
    }
}
